#name : fun binary prob diff/delta consistent for sapply
# key : probdiff.delta.consist.sapply
# contributor: Shuguang Sun
# --
${1:bdelta_consist} <- function(x) {

  ## p1 > p2, boundary > 0, to get p1 - p2 > boundary
  n1 <- x$n * x$r
  n2 <- 0:x$n
  p2c <- (0:x$n) / x$n

  ne <- ceiling(n1 * (p2c + x$boundary))
  prob = sum(dbinom(x = n2, size = x$n, prob = x$p2) *
               pbinom(q = ne - 1, size = n1, prob = x$p1, lower.tail = FALSE))

  list(n = x$n, r = x$r, p1 = x$p1, p2 = x$p2, boundary = x$boundary, prob =prob)
}

$1(x)